package com.egd.imapp.contoller.activity.login;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.egd.imapp.R;
import com.egd.imapp.contoller.activity.MainActivity;
import com.egd.imapp.model.Model;
import com.egd.imapp.model.bean.UserInfo;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.hyphenate.exceptions.HyphenateException;

/**
 * 登录页面
 */
public class LoginActivity extends Activity {
    private static final String TAG = "TAG";
    private static final int REQUEST_CODE_GO_TO_REGIST = 1;
    private static final int RESULT_OK_REGIST = -1;
    private EditText et_login_account;
    private EditText et_login_pwd;

    private Button btn_login_login;

    private TextView tv_login_register;
    private TextView tv_login_retrieve;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        // 初始化控件
        initView();
        
        // 初始化监听
        initListener();

    }

    private void initListener() {
        // 注册
        tv_login_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                Log.e(TAG, "onClick: LoginActivity tv_login_register: 注册", null);
                startActivityForResult(intent, REQUEST_CODE_GO_TO_REGIST);
            }
        });
        // 登录
        btn_login_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        // 找回密码
        tv_login_retrieve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "onClick: LoginActivity tv_login_retrieve：找回密码", null);
                Intent intent = new Intent(LoginActivity.this, RetrievePwdActivity.class);
                startActivity(intent);
            }
        });

    }

    // 登录
    private void login() {
        String username = et_login_account.getText().toString().toLowerCase();
        String password = et_login_pwd.getText().toString();

        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
            Toast.makeText(LoginActivity.this, "输入的用户名或密码不能为空", Toast.LENGTH_SHORT).show();;
            return;
        }

        final ProgressDialog pd = new ProgressDialog(LoginActivity.this);
        String ms = "正在登录...";
        pd.setMessage(ms);
        pd.setCanceledOnTouchOutside(false);
        pd.show();

        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG, "run: LoginActivity 用户登录 username = "+ username );
                // 去环信服务器登录
                EMClient.getInstance().login(username, password, new EMCallBack() {
                    //登录成功的处理
                    @Override
                    public void onSuccess() {
                        // 对模型层数据的处理
                        Model.getInstance().loginSuccess(new UserInfo(username));

                        EMClient.getInstance().chatManager().loadAllConversations();

                        // 保存用户账号信息到本地数据库 hxId 只能是小写
                        Model.getInstance().getUserAccountDao().addAccount(new UserInfo(username));

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                pd.dismiss();
                                //提示登录成功
                                Toast.makeText(LoginActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();

                                // 跳转到主页面
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);

                                // 当前页面进行销毁
                                finish();
                            }
                        });

                    }
                    // 登录失败的处理
                    @Override
                    public void onError(int code, String error) {
                        // 提示登录失败信息
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                pd.dismiss();
                                Toast.makeText(LoginActivity.this, "登录失败！" + error, Toast.LENGTH_SHORT).show();

                            }
                        });
                    }
                    // 登录过程中的处理
                    @Override
                    public void onProgress(int progress, String status) {

                    }
                });
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case REQUEST_CODE_GO_TO_REGIST:
                //判断注册是否成功  如果注册成功
                if(resultCode == RESULT_OK_REGIST){
                    //则获取data中的账号和密码  动态设置到EditText中
                    String username=data.getStringExtra("username");
                    String password=data.getStringExtra("password");
                    et_login_account.setText(username);
                    et_login_pwd.setText(password);
                }
                break;
        }

    }

    private void initView() {
        et_login_account = findViewById(R.id.et_login_account);
        et_login_pwd = findViewById(R.id.et_login_pwd);
        btn_login_login = findViewById(R.id.btn_login_login);
        tv_login_register = findViewById(R.id.tv_login_register);
        tv_login_retrieve = findViewById(R.id.tv_login_retrieve);
    }
}